home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / mint / mgr / sparcmgr / demo2.zoo / demo / ex / x.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-10-02  |  4.4 KB  |  366 lines

  1. extern char    xstr[];
  2. #line 1 "printf.c" 
  3.  
  4.  
  5.  
  6.  
  7.  
  8.  
  9.  
  10. static char *sccsid = (&xstr[8789]);
  11.  
  12. static char *printf_id = (&xstr[8849]);
  13.  
  14.  
  15. #line 1 "/usr/include/varargs.h" 1
  16.  
  17.  
  18. typedef char *va_list;
  19.  
  20.  
  21.  
  22.  
  23.  
  24.  
  25.  
  26. #line 14 "printf.c" 2
  27.  
  28.  
  29.  
  30.  
  31.  
  32.  
  33.  
  34.  
  35.  
  36.  
  37.  
  38.  
  39.  
  40.  
  41. static int width, sign, fill;
  42.  
  43. char *_p_dconv();
  44.  
  45.  
  46. ex_printf(__builtin_va_alist)
  47.     int __builtin_va_alist;
  48. {
  49.     va_list ap;
  50.     register char *fmt;
  51.     char fcode;
  52.     int prec;
  53.     int length,mask1,nbits,n;
  54.     long int mask2, num;
  55.     register char *bptr;
  56.     char *ptr;
  57.     char buf[134];
  58.  
  59.     ap = (char *) &__builtin_va_alist;
  60.     fmt = ((char * *)(ap += sizeof(char *)))[-1];
  61.     for (;;) {
  62.         
  63.         while ((fcode = *fmt++)!='%') {
  64.             
  65.             if (fcode=='\0')
  66.                 return;
  67.             ex_putchar(fcode);
  68.         }
  69.         
  70.         length = 0;
  71.         
  72.         sign = 0;
  73.         if (*fmt == '-') {
  74.             sign++;
  75.             fmt++;
  76.         }
  77.         
  78.         
  79.         fill = 1;
  80.         if (*fmt == '0') {
  81.             fill--;
  82.             fmt++;
  83.         }
  84.         
  85.         
  86.         if (*fmt == '*') {
  87.             width = (( int *)(ap += sizeof( int)))[-1];
  88.             if (width < 0) {
  89.                 width = -width;
  90.                 sign = !sign;
  91.             }
  92.             fmt++;
  93.         }
  94.         else {
  95.             width = 0;
  96.             while (*fmt>='0' && *fmt<='9')
  97.                 width = width * 10 + (*fmt++ - '0');
  98.         }
  99.         
  100.         
  101.         if (*fmt=='.') {
  102.             if (*++fmt == '*') {
  103.                 prec = (( int *)(ap += sizeof( int)))[-1];
  104.                 fmt++;
  105.             }
  106.             else {
  107.                 prec = 0;
  108.                 while (*fmt>='0' && *fmt<='9')
  109.                     prec = prec * 10 + (*fmt++ - '0');
  110.             }
  111.         }
  112.         else
  113.             prec = -1;
  114.         
  115.         
  116.  
  117.  
  118.  
  119.  
  120.  
  121.  
  122.  
  123.  
  124.  
  125.         switch (*fmt) {
  126.             case 'L':
  127.             case 'l':
  128.                 length = 2;
  129.                 
  130.             case 'h':
  131.             case 'H':
  132.                 length--;
  133.                 fmt++;
  134.                 break;
  135.         }
  136.         
  137.         
  138.  
  139.  
  140.  
  141.  
  142.         switch (fcode = *fmt++) {
  143.             
  144.             case 'c':
  145.                 buf[0] = (( int *)(ap += sizeof( int)))[-1];
  146.                 ptr = bptr = &buf[0];
  147.                 if (buf[0] != '\0')
  148.                     ptr++;
  149.                 break;
  150.             case 's':
  151.                 bptr = ((char * *)(ap += sizeof(char *)))[-1];
  152.                 if (bptr==0)
  153.                     bptr = (&xstr[8874]);
  154.                 if (prec < 0)
  155.                     prec = 32767;
  156.                 for (n=0; *bptr++ && n < prec; n++) ;
  157.                 ptr = --bptr;
  158.                 bptr -= n;
  159.                 break;
  160.             case 'O':
  161.                 length = 1;
  162.                 fcode = 'o';
  163.                 
  164.             case 'o':
  165.             case 'X':
  166.             case 'x':
  167.                 if (length > 0)
  168.                     num = ((long *)(ap += sizeof(long)))[-1];
  169.                 else
  170.                     num = (unsigned)((int *)(ap += sizeof(int)))[-1];
  171.                 if (fcode=='o') {
  172.                     mask1 = 0x7;
  173.                     mask2 = 0x1fffffffL;
  174.                     nbits = 3;
  175.                 }
  176.                 else {
  177.                     mask1 = 0xf;
  178.                     mask2 = 0x0fffffffL;
  179.                     nbits = 4;
  180.                 }
  181.                 n = (num!=0);
  182.                 bptr = buf + 11 + 3;
  183.                 
  184.                 do
  185.                     if (((int) num & mask1) < 10)
  186.                     *--bptr = ((int) num & mask1) + 060;
  187.                     else
  188.                     *--bptr = ((int) num & mask1) + 0127;
  189.                 while (num = (num >> nbits) & mask2);
  190.                 
  191.                 if (fcode=='o') {
  192.                     if (n)
  193.                         *--bptr = '0';
  194.                 }
  195.                 else
  196.                     if (!sign && fill <= 0) {
  197.                         ex_putchar('0');
  198.                         ex_putchar(fcode);
  199.                         width -= 2;
  200.                     }
  201.                     else {
  202.                         *--bptr = fcode;
  203.                         *--bptr = '0';
  204.                     }
  205.                 ptr = buf + 11 + 3;
  206.                 break;
  207.             case 'D':
  208.             case 'U':
  209.             case 'I':
  210.                 length = 1;
  211.                 fcode = fcode + 'a' - 'A';
  212.                 
  213.             case 'd':
  214.             case 'i':
  215.             case 'u':
  216.                 if (length > 0)
  217.                     num = ((long *)(ap += sizeof(long)))[-1];
  218.                 else {
  219.                     n = ((int *)(ap += sizeof(int)))[-1];
  220.                     if (fcode=='u')
  221.                         num = (unsigned) n;
  222.                     else
  223.                         num = (long) n;
  224.                 }
  225.                 if (n = (fcode != 'u' && num < 0))
  226.                     num = -num;
  227.                 
  228.                 bptr = _p_dconv(num, buf);
  229.                 if (n)
  230.                     *--bptr = '-';
  231.                 if (fill == 0)
  232.                     fill = -1;
  233.                 ptr = buf + 10 + 1;
  234.                 break;
  235.             default:
  236.                 
  237.  
  238.  
  239.                 ptr = bptr = &fcode;
  240.                 ptr++;
  241.                 break;
  242.             }
  243.             if (fcode != '\0')
  244.                 _p_emit(bptr,ptr);
  245.     }
  246.     ;
  247. }
  248.  
  249.  
  250.  
  251.  
  252.  
  253.  
  254.  
  255.  
  256.  
  257.  
  258.  
  259. char *
  260. _p_dconv(value, buffer)
  261.     long value;
  262.     char *buffer;
  263. {
  264.     register char *bp;
  265.     register int svalue;
  266.     int n;
  267.     long lval;
  268.     
  269.     bp = buffer;
  270.     
  271.     
  272.     if (value == 0) {
  273.         bp += 10;
  274.         *bp = '0';
  275.         return(bp);
  276.     }
  277.     
  278.     
  279.     n = 0;
  280.     while (value < 0) {
  281.         value -= 1000000000;    
  282.         n++;
  283.     }
  284.     while ((lval = value - 1000000000) >= 0) {
  285.         value = lval;
  286.         n++;
  287.     }
  288.     
  289.     
  290.     bp[1] = n + '0';
  291.     
  292.  
  293.  
  294.  
  295.  
  296.  
  297.     bp += 10 + 1;
  298.     while (value > 32767) {
  299.         *--bp = (int)(value % 10) + '0';
  300.         value /= 10;
  301.     }
  302.     
  303.     
  304.     svalue = value;
  305.     while (svalue > 0) {
  306.         *--bp = (svalue % 10) + '0';
  307.         svalue /= 10;
  308.     }
  309.     
  310.     
  311.     if (buffer[1] != '0') {
  312.         while (bp > buffer + 2)
  313.             *--bp = '0';
  314.         --bp;
  315.     }
  316.     return(bp);
  317. }
  318.  
  319.  
  320.  
  321.  
  322.  
  323.  
  324.  
  325.  
  326.  
  327.  
  328.  
  329.  
  330.  
  331. _p_emit(s, send)
  332.     register char *s;
  333.     char *send;
  334. {
  335.     char cfill;
  336.     register int alen;
  337.     int npad;
  338.     
  339.     alen = send - s;
  340.     if (alen > width)
  341.         width = alen;
  342.     cfill = fill>0? ' ': '0';
  343.     
  344.     
  345.     if (*s == '-' && fill < 0) {
  346.         ex_putchar(*s++);
  347.         alen--;
  348.         width--;
  349.     }
  350.     npad = width - alen;
  351.     
  352.     
  353.     if (!sign)
  354.         while (--npad >= 0)
  355.             ex_putchar(cfill);
  356.             
  357.     
  358.     while (--alen >= 0)
  359.         ex_putchar(*s++);
  360.         
  361.     
  362.     if (sign)
  363.         while (--npad >= 0)
  364.             ex_putchar(cfill);
  365. }
  366.